home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / autoCreateCharacter.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  5.3 KB  |  183 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  October, 2000
  22. //  Author:         bb
  23. //
  24. //    Procedure Name:
  25. //        autoCreateCharacter
  26. //
  27. //    Description:
  28. //        Automatically create that contains the animated channels of 
  29. //        the object. If the animated channels are already in a character,
  30. //        return the name of the existing character. If the animated channels
  31. //        are not in a character, but the object is in a character, add the
  32. //        channels to the existing character.
  33. //
  34. //    Input Arguments:
  35. //    $version: The version of this option box.  Used to know how to 
  36. //    interpret the $args array.
  37. //        "1" : $name
  38. //  
  39. //    $args
  40. //    Version 1: no args
  41. //    Version 2:
  42. //        $args[0] = $hierarchy: whether to include the hierarchy when
  43. //                creating the character
  44. //
  45. //    Return Value:
  46. //        New character name
  47. //
  48.  
  49.  
  50. global proc string autoCreateCharacter( string $version,
  51.                                         string $args[] )
  52. {
  53.     string $newCharacter = "";
  54.     string $membersToAdd[];
  55.     string $existingChars[];
  56.     string $otherChars[];    
  57.     int $memberCount = 0;
  58.     int $chCount = 0;    
  59.     string $obj, $ch;
  60.  
  61.     // check what is selected
  62.     //
  63.     string $sel[] = `ls -sl`;;
  64.     string $selInit[];
  65.     if (size($sel) == 0) {
  66.         error("Nothing is selected.");
  67.         return "";
  68.     }
  69.  
  70.     // the hierarchy option was introduced in the 2nd version of this script
  71.     //
  72.     int    $versionNo   = $version;
  73.     int    $hierarchy = 0;
  74.     if ($versionNo >= 2) {
  75.         $hierarchy = $args[0];
  76.     }
  77.  
  78.     if ($hierarchy) {
  79.         select -hi $sel;
  80.         $selInit = $sel;
  81.         $sel = `ls -sl`;
  82.     }
  83.     
  84.     // loop through the selected objects to find the animated channels
  85.     // and the existing characters on the selected objects
  86.     //
  87.     for ($obj in $sel) {
  88.         string $animPlugs[] = `listConnections -type animCurve -skipConversionNodes true -plugs true -d false -s true -c true $obj`;
  89.         string $animNodes[] = `listConnections -type animCurve -skipConversionNodes true -plugs false -d false -s true -c true $obj`;            
  90.         string $chars[] = `listConnections -s false -d true -type character $obj`;
  91.  
  92.         // First find the animatable plugs
  93.         //
  94.         int $n_anim = size($animPlugs);
  95.         int $ii;
  96.         for ($ii = 0; $ii < $n_anim; $ii += 2) {
  97.             string $animIn[] = `listConnections -s true -d false $animNodes[$ii+1]`;
  98.             if (0 == size($animIn)) {
  99.                 $membersToAdd[$memberCount++] = $animPlugs[$ii];
  100.                 for ($ch in $chars) {
  101.                     if (`character -isMember $ch $animPlugs[$ii]`) {
  102.                         $existingChars[$chCount++] = $ch;
  103.                         break;
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         for ($ch in $chars) {
  109.             $otherChars[size($otherChars)] = $ch;
  110.         }
  111.     }
  112.  
  113.     string $existingResult[];
  114.     $existingResult = AWRemoveDuplicateStringsFromStringArray($existingChars);
  115.     int $sizeExisting = size($existingResult);
  116.     if ($sizeExisting == 0) {
  117.         // none of the attributes were already in a character
  118.         //
  119.         string $otherResult[];
  120.         $otherResult = AWRemoveDuplicateStringsFromStringArray($otherChars);
  121.         if (size($otherResult) == 1) {
  122.             // but one or more of the selected objects were in a character
  123.             // so we will use the existing character
  124.             //
  125.             $sizeExisting = 1;
  126.             $existingResult[0] = $otherResult[0];
  127.         } else if (size($membersToAdd) > 0) {
  128.             // create a new character
  129.             //
  130.             // first pick a name for the character
  131.             //
  132.             string $chName = "";
  133.             string $shNames[] = `ls -sl -shortNames`;
  134.             for ($obj in $membersToAdd) {
  135.                 string $memShort[] = `ls -o -shortNames $obj`;
  136.                 string $currName = ($memShort[0]+"Ch");
  137.                 if ($chName == "" || ($currName == $chName)) {
  138.                     $chName = $currName;
  139.                 } else {
  140.                     $chName = "multiCh";
  141.                 }
  142.             }
  143.  
  144.             $newCharacter = `character -name $chName -empty`;
  145.             character -e -add $newCharacter $membersToAdd;
  146.         } else {
  147.             if ($hierarchy) {
  148.                 // replace the selection
  149.                 //
  150.                 select -r $selInit;
  151.             }
  152.             
  153.             if (size($otherResult) > 0) {
  154.                 error("The selected objects are from more than one character.");
  155.             } else {
  156.                 error("No animated channels on the selected object(s).");
  157.             }
  158.         }
  159.     }
  160.  
  161.     if ($hierarchy) {
  162.         // replace the selection
  163.         //
  164.         select -r $selInit;
  165.     }
  166.     
  167.     if ($sizeExisting == 1) {
  168.         // use the existing character, add any of the attributes that
  169.         // are not already in the character to this character
  170.         //
  171.         $newCharacter = $existingResult[0];
  172.         for ($obj in $membersToAdd) {
  173.             if (! `character -isMember $newCharacter $obj`) {
  174.                 character -e -add $newCharacter $obj;
  175.             }
  176.         }
  177.     } else if ($sizeExisting > 1) {
  178.         error("The selected objects are from more than one character.");
  179.     }
  180.  
  181.     return $newCharacter;
  182. }
  183.